﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "ElementUIUMG/Public/ElementUIBorder.h"
#include "Components/ElementUISlot.h"

UElementUIBorder::UElementUIBorder()
{
	LineColor = FLinearColor::Black;
	LineThickness = 2;
	DashedLength = 15;
	DashedInterval = 5;
	DashedOffset = 0;
	LineType = EElementUIBorederLineType::Solid;
	FilletRadius = 0.f;
	// Padding = FMargin(0);
	// HorizontalAlignment = EHorizontalAlignment::HAlign_Fill;
	// VerticalAlignment = EVerticalAlignment::VAlign_Fill;
}

void UElementUIBorder::SynchronizeProperties()
{
	Super::SynchronizeProperties();

	ElementUIBorderSlate->SetLineColor(LineColor);
	ElementUIBorderSlate->SetLineThickness(LineThickness);
	ElementUIBorderSlate->SetLineType(LineType);
	ElementUIBorderSlate->SetDashedLength(DashedLength);
	ElementUIBorderSlate->SetDashedInterval(DashedInterval);
	ElementUIBorderSlate->SetDashedOffset(DashedOffset);
	ElementUIBorderSlate->SetFilletRadius(FilletRadius);
	// ElementUIBorderSlate->SetHAlign(HorizontalAlignment);
	// ElementUIBorderSlate->SetVAlign(VerticalAlignment);
	// ElementUIBorderSlate->SetContentPadding(Padding);
}

TSharedRef<SWidget> UElementUIBorder::RebuildWidget()
{
	ElementUIBorderSlate = SNew(SElementUIBorder).LineType(LineType).LineColor(LineColor).LineThickness(LineThickness).
	                                              DashedLength(DashedLength).DashedInterval(DashedInterval).
	                                              DashedOffset(DashedOffset).FilletRadius(FilletRadius);
	if (GetChildrenCount() > 0)
	{
		Cast<UElementUISlot>(GetContentSlot())->BuildSlot(ElementUIBorderSlate.ToSharedRef());
	}
	return ElementUIBorderSlate.ToSharedRef();
}

void UElementUIBorder::PostLoad()
{
	Super::PostLoad();

	if (GetChildrenCount() > 0)
	{
		if (UPanelSlot* PanelSlot = GetContentSlot())
		{
			UElementUISlot* BorderSlot = Cast<UElementUISlot>(PanelSlot);
			if (BorderSlot == NULL)
			{
				BorderSlot = NewObject<UElementUISlot>(this);
				BorderSlot->Content = GetContentSlot()->Content;
				BorderSlot->Content->Slot = BorderSlot;
				Slots[0] = BorderSlot;
			}
		}
	}
}

void UElementUIBorder::ReleaseSlateResources(bool bReleaseChildren)
{
	Super::ReleaseSlateResources(bReleaseChildren);
	ElementUIBorderSlate.Reset();
}

UClass* UElementUIBorder::GetSlotClass() const
{
	return UElementUISlot::StaticClass();
}

void UElementUIBorder::OnSlotAdded(UPanelSlot* inSlot)
{
	UElementUISlot* BorderSlot = CastChecked<UElementUISlot>(inSlot);
	// BorderSlot->Padding = Padding;
	// BorderSlot->HorizontalAlignment = HorizontalAlignment;
	// BorderSlot->VerticalAlignment = VerticalAlignment;
	// // UE_LOG(LogTemp, Warning, TEXT("1"));
	if (ElementUIBorderSlate.IsValid())
	{
		// // UE_LOG(LogTemp, Warning, TEXT("2"));
		BorderSlot->BuildSlot(ElementUIBorderSlate.ToSharedRef());
	}
}

void UElementUIBorder::OnSlotRemoved(UPanelSlot* inSlot)
{
	if (ElementUIBorderSlate.IsValid())
	{
		ElementUIBorderSlate->SetContent(SNullWidget::NullWidget);
	}
}

#if WITH_EDITOR
const FText UElementUIBorder::GetPaletteCategory()
{
	return NSLOCTEXT("ElementUI", "ElementUICategory", "ElementUI");
}
#endif
